package com.zrf.gulimall.commnon.core.response;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 响应对象
 *
 * @author zhanrf
 * @date 2021/12/23 11:56
 */
public class Resp<T> implements Serializable {

    public final static Integer SUCCESS_CODE = 0;

    public final static Integer FAIL = 100;

    public final static String FAIL_MSG = "操作失败";

    public final static String SUCCESS_MSG = "操作成功";

    /**
     * 结果状态码
     */
    @ApiModelProperty(value = "结果状态码, 0为成功，其他为失败")
    protected Integer code;

    /**
     * 结果信息描述
     */
    @ApiModelProperty(value = "结果内容", example = SUCCESS_MSG)
    protected String msg;

    /**
     * 数据内容
     */
    @ApiModelProperty(value = "数据内容")
    protected T data;

    public static <T> Resp<T> success() {
        return new Resp<>(SUCCESS_CODE, SUCCESS_MSG);
    }

    public static <T> Resp<T> success(T data) {
        return new Resp<>(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> Resp<T> error() {
        return new Resp<>(FAIL, FAIL_MSG);
    }

    public static <T> Resp<T> error(String msg) {
        return new Resp<>(FAIL, msg);
    }

    public static <T> Resp<T> error(Integer code, String msg) {
        return new Resp<>(code, msg);
    }

    private Resp() {
    }

    private Resp(Integer code) {
        this.code = code;
    }

    private Resp(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Resp(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public Resp<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public Resp<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Resp<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
